package en.edu.lzzy.bookmanagement.controller;

import en.edu.lzzy.bookmanagement.model.Book;
import en.edu.lzzy.bookmanagement.model.Category;
import en.edu.lzzy.bookmanagement.repository.CategoryRepository;
import en.edu.lzzy.bookmanagement.service.CategoryServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Controller
@RequestMapping("/categories")
public class CategoryController {

    @Autowired
    private CategoryServiceImpl categoryRepository;

    @GetMapping("/list")
    public String getAllCategories(Model model) {
        List<Category> categories = categoryRepository.getAllCategories();
        model.addAttribute("categories", categories);
        return "category-list";
    }

    @GetMapping("/add")
    public String showAddCategoryForm(Model model) {
        Category category = new Category();
        model.addAttribute("category", category);
        return "add-category";
    }

    @PostMapping("/add")
    public String addCategory(@ModelAttribute("category") Category category) {
        categoryRepository.saveCategory(category);
        return "redirect:/categories/list";
    }

    @GetMapping("/edit/{id}")
    public String showEditCategoryForm(@PathVariable long id, Model model) {
        Category category = categoryRepository.getCategoryById(id);
        model.addAttribute("category", category);
        return "edit-category";
    }

    @PostMapping("/edit/{id}")
    public String editCategory(@PathVariable long id, @ModelAttribute("category") Category category) {
        category.setId(id);
        categoryRepository.saveCategory(category);
        return "redirect:/categories/list";
    }

    @GetMapping("/delete/{id}")
    public String deleteCategory(@PathVariable long id) {
        categoryRepository.deleteCategoryById(id);
        return "redirect:/categories/list";
    }

//    @GetMapping("/{categoryId}/books")
//    public String getBooksByCategoryId(@PathVariable long categoryId, Model model) {
//        List<Book> books = categoryRepository.getBooksByCategoryId(categoryId);
//        model.addAttribute("books", books);
//        return "book-list";
//    }
    @GetMapping("/{categoryId}/books")
    public String getBooksByCategoryId(@PathVariable long categoryId,
                                       @RequestParam(value = "page", defaultValue = "1") int page,
                                       @RequestParam(value = "pageSize", defaultValue = "999") int pageSize,
                                       Model model) {
        Page<Book> bookPage = categoryRepository.getBooksByCategoryId(categoryId, PageRequest.of(page - 1, pageSize));
        List<Book> books = bookPage.getContent();

        model.addAttribute("books", books);
        model.addAttribute("totalPages", bookPage.getTotalPages());
        model.addAttribute("totalItems", bookPage.getTotalElements());
        model.addAttribute("currentPage", page);

        return "book-list";
    }

}
